// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "house_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "房屋ID",
    "label": "房屋ID"
  },
  "user_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "用户ID",
    "label": "用户ID"
  },
  "tenant_name": {
    "rules": [
      {
        "required": true,
        "errorMessage": "租户姓名不能为空"
      },
      {
        "format": "string"
      },
      {
        "minLength": 2,
        "maxLength": 50,
        "errorMessage": "租户姓名不能少于2个字符"
      }
    ],
    "title": "租户姓名",
    "label": "租户姓名"
  },
  "tenant_phone": {
    "rules": [
      {
        "required": true,
        "errorMessage": "租户手机号不能为空"
      },
      {
        "format": "string"
      },
      {
        "pattern": "^1[3-9]\\d{9}$",
        "errorMessage": "手机号格式不正确"
      }
    ],
    "title": "租户手机号",
    "label": "租户手机号"
  },
  "tenant_id_card": {
    "rules": [
      {
        "required": true,
        "errorMessage": "租户身份证不能为空"
      },
      {
        "format": "string"
      }
    ],
    "title": "租户身份证",
    "label": "租户身份证"
  },
  "lease_start": {
    "rules": [
      {
        "required": true,
        "errorMessage": "租赁开始时间不能为空"
      },
      {
        "format": "timestamp"
      }
    ],
    "title": "租赁开始时间",
    "label": "租赁开始时间"
  },
  "lease_end": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "租赁结束时间",
    "label": "租赁结束时间"
  },
  "rent_amount": {
    "rules": [
      {
        "format": "int"
      },
      {
        "minimum": 100,
        "maximum": 100000,
        "errorMessage": "月租金不能低于100元"
      }
    ],
    "title": "月租金",
    "label": "月租金"
  },
  "deposit_type": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "押金方式",
    "label": "押金方式"
  },
  "deposit_amount": {
    "rules": [
      {
        "format": "int"
      },
      {
        "minimum": 0
      }
    ],
    "title": "押金金额",
    "label": "押金金额"
  },
  "status": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "生效中",
            "text": "生效中"
          },
          {
            "value": "已到期",
            "text": "已到期"
          },
          {
            "value": "已终止",
            "text": "已终止"
          }
        ]
      }
    ],
    "title": "租赁状态",
    "defaultValue": "生效中",
    "label": "租赁状态"
  }
}

const enumConverter = {
  "status_valuetotext": {
    "生效中": "生效中",
    "已到期": "已到期",
    "已终止": "已终止"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
